﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace SmartBot
{
    /// <summary>
    /// Chứa thông tin và function xử lý target đọc và ghi memory vào pipe
    /// </summary>
    public class TargetProcess: AllEnums
    {
        

        /// <summary>
        /// Mặc định thread delay cho Dll trong target là 300 ms. Sử dụng command x,ChangeDelay,500 to change delay to 500.
        /// </summary>
        public int ThreadDelay = 300;

        /// <summary>
        /// Process ID of the target
        /// </summary>
        public int ProcessID = 0;

        /// <summary>
        /// Name pipe sử dụng cho target là gì. File này được generate ngẫu nhiên từ function GenerateRandomName().
        /// </summary>
        public string PipeName = "";
        /// <summary>
        /// View of the name pipe file
        /// </summary>
        IntPtr pView = IntPtr.Zero;
        /// <summary>
        /// Handle of the shared mapped file
        /// </summary>
        IntPtr hMapFile = IntPtr.Zero;
        /// <summary>
        /// Trả về TRUE nếu Dll đã inject thành công
        /// </summary>
        public InjectionStatus Status = InjectionStatus.Started;

        /// <summary>
        /// Read string out of the name pipe
        /// </summary>
        /// <param name="pView">View of the name pipe to read</param>
        /// <returns>String result from the name pipe</returns>
        public static String ReadNamePipe(ref IntPtr pView)
        {
            if (pView != IntPtr.Zero)
                return (String)Marshal.PtrToStringAuto(pView);

            return "";
        }

        /// <summary>
        /// Write a string content to name pipe. Dùng function này để gửi command đến Dll process.
        /// </summary>
        /// <param name="pView">View of the name pipe. Should get from InitiateNamePipe()</param>
        /// <param name="content">String content to write to name pipe</param>
        public static void WriteNamePipe(ref IntPtr pView, string content)
        {
            //String data = (String)Marshal.PtrToStringAuto(pView);
            if (!content.EndsWith(";"))
                content += ";";

            if (content.Length <= AutoAccount.BUFF_SIZE / 2)
            {
                IntPtr address = Marshal.StringToHGlobalAuto(content);
                DLLInjectHelper.CopyMemory(pView, address, (uint)content.Length * 2);
            }
        } //End function


    }
}
